package com.scalar.chiptrack.operations.shippingRequest.actionCommands;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.commons.Address;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.shippingRequest.ShippingRequest;
import com.scalar.chiptrack.operations.shippingRequest.dao.ShippingRequestDAO;
import com.scalar.chiptrack.operations.shippingRequest.forms.ShippingRequestForm;
import com.scalar.chiptrack.operations.vendor.dao.ShipperDAO;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;

/**
 * Created by ChandraSekahr.
 * User: Administrator
 * Date: Nov 17, 2005
 * Time: 7:35:53 PM
 */
public class ShippingRequestCreateCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/shippingRequest.do";
    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws ScalarException
    {
        ShippingRequestForm shippingRequestForm     = ( ShippingRequestForm ) form;
        ShippingRequest shippingRequest             = shippingRequestForm.getShippingRequest();
        ActionForward actionForward                 = mapping.findForward( "createRequest" );
        HttpSession mSession                        = request.getSession();
        User user                                   = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );
        DBTransaction dbTransaction                 = null;
        HashMap errors                              = new HashMap();
        HashMap info                                = new HashMap();
        request.setAttribute( INFO_MESSAGES, info );
        request.setAttribute( ERROR_MESSAGES, errors );

        if ( user == null )
        {
            mSession.setAttribute ( SessionKeys.SOURCE_PAGE_KEY,  ACTION_NAME );
            return ( mapping.findForward ( "login" ) );
        }
        else
        {
            if ( user.isActionAllowed ( Functionality.SORT_RELEASE , User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute ( "showMessage", "FALSE" );
            }
            else
            {
                request.setAttribute ( "showMessage", "TRUE" );
            }
        }
        try
        {
            //retreiving new ship request number
            shippingRequest.setShipReqNumber( ShippingRequestDAO.genarateAutomatedShipRequestNumber( 1 ) );
            //retreiving the ship req date as current date
            shippingRequest.setShipReqDate( StringUtils.formatDateToString( new Date(), "MM/dd/yyyy" ) ) ;
            shippingRequest.setFromVendorName( ShippingRequestDAO.getVendorName( shippingRequest.getFromLocationId() ) );
            //retriving From VENDOR ADDDRESS AND EMAIL
            Address fromAddress = ShippingRequestDAO.getVendorAddress( shippingRequest.getFromLocationId() ) ;
            if ( fromAddress != null )
            {
                shippingRequest.setFromVendorAddress( fromAddress );
            }
            //retriving From VENDOR ADDDRESS AND EMAIL
            Address toAddress = ShippingRequestDAO.getVendorAddress( shippingRequest.getShipTolocationId() ) ;
            if ( toAddress != null )
            {
                shippingRequest.setToVendorAddress( toAddress );
            }
            //SHOW ALSO TO VENDOR ADDRESS AND EMAIL
            shippingRequest.setToVendorName( ShippingRequestDAO.getVendorName( shippingRequest.getShipTolocationId() ) );
            //setting the part number
            shippingRequest.setPartNumber( ProductDao.getProductNameByProcessStep_Id( shippingRequest.getProcessStepId() ) );

            ShippingRequestDAO.loadShipAlertMails( null, shippingRequest );

            ArrayList lotsList = ShippingRequestDAO.getLotDetails( shippingRequest );

            if( ( lotsList != null ) && ( lotsList.size() > 0 ) )
            {
                shippingRequest.setLotList( lotsList );
                shippingRequest.setShippersList( ShipperDAO.loadShippersForVendor( null, ShippingRequestDAO.getVendorIdByLocationId( shippingRequest.getFromLocationId() ) ) );
                shippingRequestForm.setShippingRequest( shippingRequest );
            }
            else
            {
                info.put( "No Lots Available for the given search criteria",null );
                request.setAttribute( "partNumberList", ShippingRequestDAO.getProductList( Constants.LOCATION_TYPE_CLASS_STORE ) );
                request.setAttribute( "vendorList", ShippingRequestDAO.loadClassStroeLocations( null )  );
                request.setAttribute( "shipToLocationList", ShippingRequestDAO.loadClassStroeLocations( null ) );
                actionForward = mapping.findForward( "newRequest" );
            }

        }
        catch( Exception e )
        {
            errors.put( "Error while retreiving the Lot Number list", null );
        }

        Logger.debug( "Fowarding the request to the create page" );
        saveToken( request );
        return actionForward;
    }

}
